let app = getApp()
const cookieUtil = require("../../utils/cookie.js")

Page({
	data: {
		registerButtonStatus: true,
		userInfoDict: {
			avatartUrl: "https://pic1.zhimg.com/80/v2-6ac760cd2a2f63b910aa6417bad9e117_1440w.jpg",
			nickName: "阿波罗尼斯圆",
			userDesc: "这个人很懒，什么都没有留下哦",
			backgroundImageUrl: "https://pic4.zhimg.com/80/v2-1af39af284bab46846563c458399b628_1440w.jpg"
		},
		headerBarLists: [{
			thumbSrc: "https://gw.alipayobjects.com/mdn/rms_815f8a/afts/img/A*ByjGRLRxLecAAAAAAAAAAABkARQnAQ",
			text: "我的活动",
			navigateUrl: "/pages/myactivity/myactivity"
		}],
		bottomBarLists: [{
				thumbSrc: "https://gw.alipayobjects.com/mdn/rms_815f8a/afts/img/A*kcErQZFFbPMAAAAAAAAAAABkARQnAQ",
				text: "我的信息",
				navigateUrl: "/pages/myinfo/myinfo"
			},
			{
				thumbSrc: "https://gw.alipayobjects.com/mdn/rms_815f8a/afts/img/A*rpcLTLE2M58AAAAAAAAAAABkARQnAQ",
				text: "用户反馈",
				navigateUrl: "/pages/myservice/myservice"
			},
			{
				thumbSrc: "https://gw.alipayobjects.com/mdn/rms_815f8a/afts/img/A*LjmkRpZoeDMAAAAAAAAAAABkARQnAQ",
				text: "关于我们",
				navigateUrl: "/pages/aboutus/aboutus"
			}
		]
	},
	OnDoNothing () {
		//Do nothing
	},
	onLoad(options) {
		
		const url = app.globalData.baseUrl + app.globalData.registerUrl
		//从缓存中拿取cookie
		const cookie = cookieUtil.getCookieFromStorage().data
		//如果不存在cookie则让其注册/登录
		if( !cookie ){
			my.navigateTo({
				url:"/pages/register/register"
			})	
		}
		
		//判断服务端session有无过期
		let promise = new Promise((resolve, reject) => {
			
			let headers = {}
			headers.cookie = cookie
			
			my.request({
				url,
				headers,
				method: "GET",
				success: (res) => {
					resolve(res)
				}
			})
		})
		
		promise.then((res) => {
			console.log(res)
			//如果未登录则进行跳转
			if (res.data.status == "fail") {
				my.navigateTo({
					url:"/pages/register/register"
				})		
			}
		})

				
		if (app.globalData.userInfo) {
			let {avatar,nick,nickName} = app.globalData.userInfo
			let userInfoDict = this.data.userInfoDict
			
			userInfoDict.avatartUrl = avatar
			userInfoDict.nickName = nick || nickName
			
			this.setData({
				registerButtonStatus: false,
				userInfoDict
			})
		}
	},
	onGetAuthorize(res) {
		my.getOpenUserInfo({
			fail: (res) => {},
			success: (res) => {
				let userInfo = JSON.parse(res.response).response // 以下方的报文格式解析两层 response
				//有选择的获取用户数据
				let {
					avatar,
					city,
					gender,
					nickName,
					province
				} = userInfo

				//更新到视图中
				let userInfoDict = this.data.userInfoDict
				userInfoDict.avatartUrl = avatar
				userInfoDict.nickName = nickName
				this.setData({
					userInfoDict,
					registerButtonStatus: false
				})
				app.globalData.userInfo = userInfoDict
			},
		});
	},
	onPreviewAvatarImage() {
		my.previewImage({
			current: 0,
			urls: [
				this.data.userInfoDict.avatartUrl
			],
		})
	},
	onPreviewbackgroundImageUrl() {
		my.previewImage({
			current: 0,
			urls: [
				this.data.userInfoDict.backgroundImageUrl
			],
		})
	},
	onheaderBarClick(e) {
		const index = e.index
		const url = this.data.headerBarLists[index].navigateUrl
		my.navigateTo({
			url
		})
	},
	onbottomBarClick(e) {
		const index = e.index
		const url = this.data.bottomBarLists[index].navigateUrl
		my.navigateTo({
			url
		})
	},
});
